/*
 * Pressure_A.c
 *
 * Created: 3/21/2013 8:01:11 AM
 *  Author: Parag
 */ 

#include "Pressure_A.h"

void ADCSetup() {
	
	DIDR0 = ~(0b11111101);
	int refSel_2b=1;    //select avcc, change to 3 for 1.1V reference
	int adlar_1b=0;     //no need to left shift
	int muxSel_5b = 1;  //select ADC0 with single-ended conversion
	ADMUX = (refSel_2b << REFS0) + (adlar_1b<<ADLAR) + (muxSel_5b << MUX0);

	//enable adc and with interrupt disabled
	ADCSRA = (1<<ADEN) + (0 << ADSC) + (0 << ADATE) + (0 << ADIF) + (0<< ADIE) + (0 <<ADPS0);
}

//Call this function to start an ADC conversion
void ADCStartConversion(){
	ADCSRA |= 1 << ADSC; //this is automatically cleared
}


void ADCClearConversionFlag(){
	ADCSRA |= (1<<ADIF); //all interrupt flags are cleared by writing a one
}

int ADCIsConversionCompleteFlagSet(){
	return(ADCSRA & (1<<ADIF));
}


/*
ADCAquire()
PreCondition: none
Post Condition: returns the voltage reading
Note: Voltage read is always 20% of the original and is compared to VCC or ARef
*/
uint16_t ADCAquire(){
	uint16_t result;
	result = 0;
	ADCClearConversionFlag();
	ADCStartConversion();
	while(! ADCIsConversionCompleteFlagSet() );
	result = ADCL;            // read out ADCL register
	result += (ADCH << 8);    // read out ADCH register
	return result;
	
}